<template>
  <div class="init-container">
    <div class="top-card-box">
      <o-card-box position="top" :width="150" :height="30">
        <span class="top-card-box-title">
          <span>关联点位查询</span>
        </span>
      </o-card-box>
    </div>
    <div class="mask-container" v-if="isMask"></div>

    <vxe-grid
      ref="xGrid"
      v-bind="gridOptions"
      @page-change="pageChange"
    ></vxe-grid>
  </div>
</template>

<script lang="ts">
import {
  Tsrv,
  T_srv,
  ifs,
  ts,
  vue,
  getGridOptions,
  N_AssPointQuery,
  I_cmd,
  T_cmd
} from './config';

@T_srv.comp
export default class cAssPointQuery extends vue {
  /* 父组件compBoxInfo */
  @T_srv.prop({
    required: false,
    type: Object,
    default: null
  })
  compBoxInfo: ifs.I_compBoxInfo;

  /* 组件名 */
  private name: string = 'cAssPointQuery';

  private G_compName: string = this.name;

  /* 组件箱信息对象 */
  private GT_compBoxInfo: ifs.I_compBoxInfo;

  /* 组件箱对象 */
  private GT_compBox: ifs.I_compBox;

  /* 组件箱识别ID */
  private G_compID: string;

  /* 指挥部 */
  private GT_command: I_cmd = new T_cmd();

  /* 遮罩 */
  public isMask: boolean = false;

  private gridOptions = getGridOptions();

  private content: N_AssPointQuery.content[];

  private filterLable: number;

  created() {
    // 如果父组件没有传参，那就产生一个
    this.GT_compBoxInfo = Tsrv.getCompInfo(this.compBoxInfo);
    this.G_compID = this.GT_compBoxInfo.compID;
    this.GT_compBox = this.GT_compBoxInfo.TcompBox;
    // this.GT_command.init(this, this.GT_compBoxInfo);
  }

  mounted() {
    /* 向父组件发送实例 */
    this.GT_compBox.addCompMergeName(this.G_compID, this.G_compName, this);
    this.$emit('compBoxInfo', this.GT_compBoxInfo);
    this.$bus.$on('202214F34D00BB5C', (msg: any) => {
      this.handleListener(msg);
    });
  }

  destroyed() {
    this.$bus.$off('202214F34D00BB5C');
    /* 注册组件销毁 */
    this.GT_compBox.removeCompMergeName(this.G_compID, this.G_compName);

    /* 如果父组件传入的compBoxInfo则删除本文件产生的引用,反之是本组件产生的ID统一删除 */
    if (Tsrv.utils.isEmpty(this.compBoxInfo)) {
      this.GT_compBox.removeCompsWithCompID(this.G_compID);
    } else {
      this.GT_compBox.removeCompsMergeName(this.G_compID, [
        `'${this.G_compName}'`
      ]);
    }
    /* 如果是动态组件可能会关闭全局compBox */
    // this.GT_compBox = null
  }

  /* 设置组件整体遮罩 */
  public setMask(p: boolean) {
    this.isMask = p;
  }

  public getMask(p: boolean): boolean {
    return this.isMask;
  }

  private handleListener(msg: any) {
    const custMsg = Tsrv.utils.clone(msg, true);
    const res = custMsg.option.autoCompleteInfo.data;

    this.filterLable = Number(res['alarm_tag_num']);
    // 加载数据
    if (Tsrv.noValue(res?.content)) return;
    this.content = JSON.parse(res.content);
    const p: ifs.I_qryDataReq = {
      ecCodes: this.content.map(item => item.code),
      beginTime: '2024-01-01 00:00:00',
      endTime: Tsrv.utils.getCurrentTimeStrCN(),
      pageNo: 1,
      pageSize: 10,
      filter: {
        'metadata.label': {
          $in: [this.filterLable]
        }
      }
    };
    this.loadTableData(p);
  }

  /**
   * pageChange 翻页
   */
  private pageChange(e: any) {
    if (
      this.gridOptions.pagerConfig.currentPage !== e.currentPage ||
      this.gridOptions.pagerConfig.pageSize !== e.pageSize
    ) {
      this.gridOptions.pagerConfig.currentPage = e.currentPage;
      this.gridOptions.pagerConfig.pageSize = e.pageSize;
      const p: ifs.I_qryDataReq = {
        ecCodes: this.content.map(item => item.code),
        beginTime: '2024-01-01 00:00:00',
        endTime: Tsrv.utils.getCurrentTimeStrCN(),
        pageNo: e.currentPage,
        pageSize: e.pageSize,
        filter: {
          'metadata.label': {
            $in: [this.filterLable]
          }
        }
      };
      this.loadTableData(p);
    }
  }

  public loadTableData(p: ifs.I_qryDataReq) {
    Tsrv.getQueryData(p).then(res => {
      if (typeof res === 'boolean' || Tsrv.noValue(res)) return;
      const data: Record<string, N_AssPointQuery.QueryResult> = res;

      const rowData: N_AssPointQuery.rowData[] = [];
      Object.keys(data).forEach(key => {
        if (!Tsrv.noValue(data[key].d)) {
          data[key].d.forEach(d => {
            const obj: N_AssPointQuery.rowData = {
              t: d.t,
              v:
                d.v /
                this.content.find(item => item.code === key)?.conversion_rate,
              label: d.label,
              f_name: this.content.find(item => item.code === key)?.title
            };

            rowData.push(obj);
          });
        }
      });

      const totals = Object.values(data).reduce(
        (pre, cur) => pre + cur.total,
        0
      );
      this.gridOptions.pagerConfig.total = totals;
      const grid: any = this.$refs.xGrid;
      grid.reloadData(rowData);
    });
  }
}
</script>
<style lang="scss" scoped>
.top-card-box {
  position: relative;
  display: flex;
  justify-content: center; /* 水平居中 */
  .top-card-box-title {
    display: flex;
    justify-content: center; /* 水平居中 */
  }
}
</style>
